package com.yonyou.lxp.account.adapter;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.yonyou.lxp.account.R;
import com.yonyou.lxp.account.entity.ExpenditureInfo;
import com.yonyou.lxp.account.utils.Constants;
import com.yonyou.lxp.account.views.MyTextView;

import org.xutils.x;

import java.util.Date;
import java.util.List;

import me.drakeet.materialdialog.MaterialDialog;

/**
 * Created by liuxiaopeng on 16/2/28.
 */
public class MyMsgAdapter extends BaseAdapter {
    private List<ExpenditureInfo> list;
    private LayoutInflater inflater;
    private Activity context;

    public MyMsgAdapter(List list, LayoutInflater inflater, Activity context) {
        this.list = list;
        this.context = context;
        this.inflater = inflater;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public ExpenditureInfo getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHelder viewHelder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_e, null);
            viewHelder = new ViewHelder();
            viewHelder.tv_item_money = (TextView) convertView.findViewById(R.id.tv_item_money);
            viewHelder.tv_item_time = (TextView) convertView.findViewById(R.id.tv_item_time);
            viewHelder.tv_item_name = (TextView) convertView.findViewById(R.id.tv_item_name);
            viewHelder.tv_item_type = (TextView) convertView.findViewById(R.id.tv_item_type);
            viewHelder.tv_item_remarks = (MyTextView) convertView.findViewById(R.id.tv_item_remarks);
            convertView.setTag(viewHelder);
        } else {
            viewHelder = (ViewHelder) convertView.getTag();
        }


        final ExpenditureInfo item = list.get(position);
        viewHelder.tv_item_remarks.getParent().requestDisallowInterceptTouchEvent(true);
        viewHelder.tv_item_remarks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initDialog(position, item.getE_remarks());
            }
        });
        if (item != null) {
            viewHelder.tv_item_money.setText(item.getE_money());
            viewHelder.tv_item_time.setText(Constants.mFormatter.format(new Date(item.getE_time())));
            viewHelder.tv_item_name.setText(item.getT_name());
            viewHelder.tv_item_type.setText("0".equals(item.getT_type()) ? "-" : "+");
            viewHelder.tv_item_remarks.setText(item.getE_remarks());

            if ("0".equals(item.getT_type())) {
                viewHelder.tv_item_money.setTextColor(Color.RED);
                viewHelder.tv_item_time.setTextColor(Color.RED);
                viewHelder.tv_item_name.setTextColor(Color.RED);
                viewHelder.tv_item_type.setTextColor(Color.RED);
                viewHelder.tv_item_remarks.setTextColor(Color.RED);
            } else {
                viewHelder.tv_item_money.setTextColor(context.getResources().getColor(R.color.green));
                viewHelder.tv_item_time.setTextColor(context.getResources().getColor(R.color.green));
                viewHelder.tv_item_name.setTextColor(context.getResources().getColor(R.color.green));
                viewHelder.tv_item_type.setTextColor(context.getResources().getColor(R.color.green));
                viewHelder.tv_item_remarks.setTextColor(context.getResources().getColor(R.color.green));
            }
        }
        return convertView;
    }

    static class ViewHelder {
        TextView tv_item_money;
        TextView tv_item_time;
        TextView tv_item_name;
        TextView tv_item_type;
        MyTextView tv_item_remarks;
    }

    private EditText editText;
    private AlertDialog dialog;
    private AlertDialog.Builder builder;

    private void initDialog(final int positon, final String remarks) {
        builder = new AlertDialog.Builder(context);

        editText = new EditText(context);
        editText.setText(remarks);
        builder.setTitle("备注修改");
        builder.setView(editText);

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();
            }
        });
        builder.setNeutralButton("清空", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();
                list.get(positon).setE_remarks(editText.getText() + "");
                MyMsgAdapter.this.notifyDataSetChanged();
            }
        });

        dialog = builder.create();
        dialog.show();


        dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText.setText("");
            }
        });
    }
}
